//
//  SYHomeViewController.swift
//  SYWeibo
//
//  Created by Eric on 16/7/19.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit
//cell 可重用标识符
private let homeCellId = "homeCellId"

class SYHomeViewController: SYVisitorViewController {
    
    //全局的模型数组
    lazy var statusListVeiwModel:SYStatusListViewModel = SYStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin{
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            return
            
        }
        setTableViewInfo()
        getHomeData()
    }
    /**
     reason: 'must pass a class of kind UITableViewHeaderFooterView'
     */
     
     //设置tableView
    func setTableViewInfo(){
//把要添加的nav 下面的   提示 更新数据条 添加 NavigationController 上
 
      navigationController?.view.insertSubview(pullDownTipLabel, belowSubview: (navigationController?.navigationBar)!)
        
        
        //数据源代理方法
        tableVeiw.dataSource = self
        tableVeiw.delegate = self
        tableVeiw.backgroundColor = UIColor.grayColor()
        //注册cell
        tableVeiw.registerClass(SYStautsCell.self, forCellReuseIdentifier: homeCellId)
        tableVeiw.estimatedRowHeight = 200
        //分离tableView 上的 线
        tableVeiw.separatorStyle = .None
        //设置tableVeiw footView
        
        tableVeiw.tableFooterView = indicatorView
        //  添加刷新控件
        tableVeiw.addSubview(refreshControl)
        // 添加事件
        refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
    }
    //监听点击方法
    func refreshAction(){
        getHomeData()
    }
    
    // MARK: - 懒加载控件
    // 菊花
    private lazy var indicatorView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        // 给菊花设置颜色
        view.color = ThemeColor
        
        return view
    }()
    // 系统下载刷新
    private lazy var refreshControl: SYRefreshControl = SYRefreshControl()
    //请求微博数据提示
    private lazy var pullDownTipLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.whiteColor(), textFont: 12)
        lab.frame = CGRect(x: 0, y: 64 - 30, width: SCREENW, height: 30)
        lab.textAlignment = .Center
        lab.backgroundColor = ThemeColor
        lab.hidden = true
        return lab
    }()
}




// MARK :  -UITableViewDataSource

extension SYHomeViewController: UITableViewDataSource,UITableViewDelegate{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListVeiwModel.statusList.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let  cell = tableVeiw.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as! SYStautsCell
        cell.statusViewModel = statusListVeiwModel.statusList[indexPath.row]
        
        return cell
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        //如果判断当前显示的是最后一个cell 的时候 才可以叫菊花转 且 当前菊花没有开始动画
        if indexPath.row == statusListVeiwModel.statusList.count - 1 && !indicatorView.isAnimating(){
            // 开始动画
            indicatorView.startAnimating()
            getHomeData()
        }
    }
    
}


//MARK :  -请求微博数据
extension SYHomeViewController{
    
    //请求首页数据
    func getHomeData(){
        statusListVeiwModel.loadHomeData(indicatorView.isAnimating()){ (isSuc, count) -> () in
            //停止动画
            self.endRefreshing()
            if !isSuc{
                print("请求数据失败")
                return
            }
            self.tableVeiw.reloadData()
            //下拉刷新
            if count >= 0{
            self.setpullDownTipLabelAnim(count)
            
            }
        }
    }
    
    //设置微博提示动画
    func setpullDownTipLabelAnim(count: Int){
    //判断如果当前在动画也就是显示 就不在进行动画
        if pullDownTipLabel.hidden == false{
        return
        }
        //显示空间
        pullDownTipLabel.hidden = false
        var name = ""
        if count == 0 {
        name = "您已经是最新的微博数据啦"
        }else{
        name = "您更新了\(count)条数据"
        }
        //设置text
        pullDownTipLabel.text = name
        //设置动画
        UIView.animateWithDuration(0.5, animations: { () -> Void in
        //提示 更新数据条在 移动的位置
            self.pullDownTipLabel.transform = CGAffineTransformMakeTranslation(0, 30)
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, delay: 1, options: [], animations: { () -> Void in
                    self.pullDownTipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        // 完成后隐藏
                        self.pullDownTipLabel.hidden = true
                })
        }
    
    }
    
    // 停止各种动画
    func endRefreshing(){
        self.indicatorView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
    
}



















